React input re-render 問題


Posted by Rich on 2021-09-02

在串 google 地圖 api 中增加搜尋列。但發現搜尋列每輸入一個值就會因為頁面重新渲染,造成 losing focus ,每打一個字就會歸零。
解決:方法來源 只要把 input 宣告在外面就可以了。
原因:因為把 input 宣告在 render 的 function 裡面。由於 re-render 就是重新呼叫一次函式,所以將 input 宣告在 function 裡面會變成重新宣告一個新的 input。所以每次 re-render 都會有一個新的 input。


#React







Related Posts

Day02 重裝待發

Day02 重裝待發

F2E合作社|按鈕元件|Bootstrap 5網頁框架開發入門

F2E合作社|按鈕元件|Bootstrap 5網頁框架開發入門

const 斷言 & infer 關鍵字

const 斷言 & infer 關鍵字


Comments